package main

import (
	"fmt"
	"math/rand"
	"strconv"
	"strings"
)

//生成包含100个随机元素的slice
func genSlice(sli []int) []int {
	for i := 0; i < 100; i++ {
		sli = append(sli, rand.Intn(128))
	}
	return sli
}

/*
构造map实现slice去重
map的key不会重复，通过此特性获取到map的长度，
就可以取到slice去重后个数
*/
func dupEle(sli []int) int {
	m2 := make(map[int]bool)
	// var int count
	for _, ele := range sli {
		m2[ele] = true
	}
	return len(m2)
}

//输入[]{2,4,6}，返回"2 4 6"
func arr2string(arr []int) string {
	sb := strings.Builder{}
	sb.WriteString("\"")
	for i := 0; i < len(arr); i++ {
		if i == len(arr)-1 {
			sb.WriteString(strconv.Itoa(arr[i]))
		} else {
			sb.WriteString(strconv.Itoa(arr[i]))
			sb.WriteString(" ")
		}
	}
	sb.WriteString("\"")
	return sb.String()
}

func main() {
	slice1 := make([]int, 0, 10)
	fmt.Println(dupEle(genSlice(slice1))) //去重

	arr1 := []int{2, 4, 6}
	arr2 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
	arr3 := []int{} //空slice

	fmt.Println(arr2string(arr1))
	fmt.Println(arr2string(arr2))
	fmt.Println(arr2string(arr3)) //传入空slice
}
